package top.shiyiri.utils;

import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

/**
 * @author Aunean
 * @date 2022/2/1 16:11
 */
public class WebUtils {

    public static <T> T copyParamsToBean(HttpServletRequest req, T bean) {

        try {
            BeanUtils.populate(bean, req.getParameterMap());
        } catch (IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
        return bean;
    }

    public static <T> T copyParamsToBean(Map value, T bean) {

        try {
            BeanUtils.populate(bean, value);
        } catch (IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
        return bean;
    }
}
